<?php

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class SimplelistingControllerFields extends JController
{
    
	function __construct( $default = array() )
	{
		if ( ! JRequest::getCmd( 'view' ) ) {
			
			JRequest::setVar('view', 'fields' );
		}
		
		parent::__construct( $default );

		$this->registerTask( 'apply', 'save' );
		$this->registerTask( 'add', 'edit' );
	}

        function display()
        {
            JRequest::setVar('view', 'fields');
            parent::display();
        }
	
	function edit()
	{
		//create the view
		$view = & $this->getView('fields', 'html');
		
		// Get/Create the model
		$model = & $this->getModel('Fields');
		
		// Push the model into the view (as default)
		$view->setModel($model, true);
		
		$view->display('form');
	}
	
	
	function save()
	{
		// Check for request forgeries
//		JRequest::checkToken() or die( 'Invalid Token' );
		
		// Get/Create the model
		$model = & $this->getModel('Fields');
		
		$url = 'index.php?option=com_simplelisting&controller=fields' ;
		
		if ( false ===  $model->save() ) {

		    $this->edit();
		    
		} else {

		  	if ( 'apply' == $this->getTask() ) {
		  		
		  		$row =& $model->getRow();
		  		$url = $url . '&task=edit&id=' . $row->id ;
		  	}
		  	
                    $msg = JText::_( 'SAVED_FIELD_MSG' ) ;
		    $this->setRedirect( $url , $msg );
		}
	}
	
	
	function cancel()
	{
		$this->_setDefaultRedirect();
	}
	
	
	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );
		
		$model = & $this->getModel('Fields');
		
		if ( $model->remove() ) {
			
			$msg = JText::sprintf('ITEM_DELETED_MSG', count($model->getCid()));
			$this->setRedirect( 'index.php?option=com_simplelisting&controller=fields', $msg );
			
		} else {
			
			$this->_setDefaultRedirect();
		}	
	}
	
	function orderdown()
	{
		$this->_order(1);
	}
	
	function orderup()
	{
		$this->_order(-1);
	}
	
	/******************************  Private functions   *****************************************/
	

	function _order( $inc )
	{
	    $model = & $this->getModel('Fields');
		$row =& $model->getRow();
		$row->move( $inc );
		$this->_setDefaultRedirect();
	}
	
	function _setDefaultRedirect()
	{
		$this->setRedirect( 'index.php?option=com_simplelisting&controller=fields' );
	}

        function saveorder()
	{
        $model = & $this->getModel('Fields');
        $table =& $model->getSqlTableName();
        $cid = JRequest::getVar( 'cid', array(0), '', 'array' );
	JArrayHelper::toInteger($cid, array(0));
	//$this->cids = implode( ',', $cid );
	$order		= JRequest::getVar( 'order', array(0), 'post', 'array' );
	//$itemid		= JRequest::getVar( 'itemid', array(0), 'post', 'array' );
	foreach($cid as $cids=>$c){
	$query = 'UPDATE ' . $table
		. ' SET ordering = \'' . (int) $order[$cids]
		. '\' WHERE id = '. $c//$itemid[$cids-1]
		;
	$db 	=& JFactory::getDBO();
		$db->setQuery( $query );
		if (!$db->query())
		{
			JError::raiseError(500, $db->getErrorMsg() );
		}

	}
	$order = JRequest::getVar( 'order', array(0), 'post', 'array' );

		$this->_setDefaultRedirect();
}

    function publish()
    {
        $cid 	= JRequest::getVar( 'cid', array(0), 'default', 'array' );

        if($cid === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = $this->getModel('fields');
        $model->publish($cid, 1);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&controller=fields');
        $this->setRedirect($redirectTo,  JText::_('PUBLISH_ITEM_MSG'));
    }

    function unpublish()
    {
        $cid 	= JRequest::getVar( 'cid', array(0), 'default', 'array' );

        if($cid === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = $this->getModel('fields');
        $model->publish($cid, 0);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&controller=fields');
        $this->setRedirect($redirectTo, JText::_('UNPUBLISH_ITEM_MSG'));
    }
	
	
}
